var t = require("../../config.js"), e = require("./utils/aes_util.js");

Component({
    options: {
        multipleSlots: !0
    },
    properties: {
        opt: {
            type: Object,
            value: null
        }
    },
    data: {
        show: !1,
        token: "",
        secretKey: "",
        originalImageBase64: "",
        jigsawImageBase64: "",
        verifyImgOutHeight: "",
        verifySubBlockWidth: "",
        verifySubBlockTop: "",
        leftBarClass: "status-1",
        verifyTipsClass: "",
        verifyTipsText: "",
        verifyMsgText: "",
        verifyBarAreaClass: "",
        clickWordTapName: "",
        clickWordTapNum: 0,
        clickWordXYList: [],
        clickWordPointList: [ {
            left: 0,
            top: 0,
            display: "none"
        }, {
            left: 0,
            top: 0,
            display: "none"
        }, {
            left: 0,
            top: 0,
            display: "none"
        }, {
            left: 0,
            top: 0,
            display: "none"
        }, {
            left: 0,
            top: 0,
            display: "none"
        } ],
        backImgLeft: 0,
        backImgTop: 0
    },
    lifetimes: {
        attached: function() {
            var e = this, a = {
                baseUrl: t.config.base_url,
                mode: "pop",
                captchaType: "blockPuzzle",
                imgSize: {
                    width: "310px",
                    height: "155px"
                },
                barHeight: "40px",
                vSpace: 5,
                success: function(t) {},
                fail: function(t) {}
            };
            console.log(e.data.opt);
            var o = e._extend(a, e.data.opt);
            console.log(o), "clickWord" == o.captchaType && "fixed" == o.mode && wx.createSelectorQuery().in(e).select(".backImg").boundingClientRect(function(t) {
                console.log(t), e.setData({
                    backImgLeft: t.left,
                    backImgTop: t.top
                });
            }).exec(), e.setData({
                verifyImgOutHeight: parseInt(o.imgSize.height) + parseInt(o.vSpace) + "px",
                verifySubBlockWidth: Math.floor(47 * parseInt(o.imgSize.width) / 310) + "px",
                verifySubBlockTop: "-" + (parseInt(o.imgSize.height) + parseInt(o.vSpace)) + "px",
                show: "pop" != o.mode
            }), e._uuid(), e._refresh();
        }
    },
    methods: {
        switchType: function() {
            this.triggerEvent("switchType");
        },
        show: function() {
            var t = this;
            this.setData({
                show: !0
            }, function() {
                "clickWord" == t.data.opt.captchaType && "pop" == t.data.opt.mode && wx.createSelectorQuery().in(t).select(".backImg").boundingClientRect(function(e) {
                    console.log(e), t.setData({
                        backImgLeft: e.left,
                        backImgTop: e.top
                    });
                }).exec();
            });
        },
        hide: function() {
            this.setData({
                show: !1
            });
        },
        reload: function() {
            var t = this, e = t.data.opt;
            console.log(e), "clickWord" == e.captchaType && "fixed" == e.mode && wx.createSelectorQuery().in(t).select(".backImg").boundingClientRect(function(e) {
                console.log(e), t.setData({
                    backImgLeft: e.left,
                    backImgTop: e.top
                });
            }).exec(), t.setData({
                verifyImgOutHeight: parseInt(e.imgSize.height) + parseInt(e.vSpace) + "px",
                verifySubBlockWidth: Math.floor(47 * parseInt(e.imgSize.width) / 310) + "px",
                verifySubBlockTop: "-" + (parseInt(e.imgSize.height) + parseInt(e.vSpace)) + "px",
                show: "pop" != e.mode
            }), t._refresh();
        },
        _blockPuzzleCheck: function(t) {
            console.log("开始校验：" + JSON.stringify(t));
            var a = this;
            console.log(a.data.opt);
            var o = a.data.opt.baseUrl + "/captcha/check", i = i = "blockPuzzle" == a.data.opt.captchaType ? wx.getStorageSync("slider") : wx.getStorageSync("point"), s = t.offsetX / parseInt(a.data.opt.imgSize.width) * 310;
            console.log("移动距离:" + t.offsetX + "\t换算比例距离X:" + s);
            var c = JSON.stringify({
                x: s,
                y: 5
            }), r = {
                captchaType: a.data.opt.captchaType,
                pointJson: a.data.secretKey ? e.AesEncrypt(c, a.data.secretKey) : c,
                token: a.data.token,
                clientUid: i,
                ts: Date.now()
            };
            console.log(r), a._postData(o, r, function(o) {
                "0000" == o.data.repCode ? (console.log(t.expendTime + "s验证成功"), a.setData({
                    leftBarClass: "status-4",
                    verifyTipsClass: "suc-bg",
                    verifyTipsText: t.expendTime + "s验证成功"
                }), setTimeout(function() {
                    a._refresh(), "pop" == a.data.opt.mode && a.hide();
                    var t = a.data.token + "---" + c;
                    t = a.data.secretKey ? e.AesEncrypt(t, a.data.secretKey) : t, "function" == typeof a.data.opt.success && a.data.opt.success({
                        captchaVerification: t
                    });
                }, 700)) : (a.setData({
                    leftBarClass: "status-3",
                    verifyTipsClass: "err-bg",
                    verifyTipsText: o.data.repMsg
                }), "function" == typeof a.data.opt.fail && a.data.opt.fail(o.data), setTimeout(function() {
                    a._refresh();
                }, 700));
            });
        },
        _clickWordTap: function(t) {
            var a = this;
            console.log(t);
            var o = t.detail.x, i = t.detail.y;
            if (a._setPoint(o, i), console.log(a.data.clickWordTapNum), console.log(a.data.clickWordXYList), 
            a.data.clickWordTapNum > 2) {
                a.setData({
                    clickWordTapName: ""
                });
                var s = a.data.opt.baseUrl + "/captcha/check", c = c = "blockPuzzle" == a.data.opt.captchaType ? wx.getStorageSync("slider") : wx.getStorageSync("point"), r = JSON.stringify(a.data.clickWordXYList), n = {
                    captchaType: a.data.opt.captchaType,
                    pointJson: a.data.secretKey ? e.AesEncrypt(r, a.data.secretKey) : r,
                    token: a.data.token,
                    clientUid: c,
                    ts: Date.now()
                };
                console.log(n), a._postData(s, n, function(t) {
                    "0000" == t.data.repCode ? (a.setData({
                        verifyMsgText: "验证成功",
                        verifyBarAreaClass: "suc-area"
                    }), setTimeout(function() {
                        a._refresh(), "pop" == a.data.opt.mode && a.hide();
                        var t = a.data.token + "---" + r;
                        t = a.data.secretKey ? e.AesEncrypt(t, a.data.secretKey) : t, "function" == typeof a.data.opt.success && a.data.opt.success({
                            captchaVerification: t
                        });
                    }, 700)) : (a.setData({
                        verifyMsgText: "验证失败",
                        verifyBarAreaClass: "err-area"
                    }), "function" == typeof a.data.opt.fail && a.data.opt.fail(t.data), setTimeout(function() {
                        a._refresh();
                    }, 700));
                });
            }
        },
        _setPoint: function(t, e) {
            var a = this.data.clickWordXYList, o = this.data.clickWordPointList, i = t - this.data.backImgLeft, s = e - this.data.backImgTop;
            console.log("点击位置：" + t + "\t" + e + "\t底图位置：" + this.data.backImgLeft + "\t" + this.data.backImgTop);
            var c = i / parseInt(this.data.opt.imgSize.width) * 310, r = s / parseInt(this.data.opt.imgSize.width) * 310;
            a.push({
                x: c,
                y: r
            }), o[this.data.clickWordTapNum] = {
                left: i - 10 + "px",
                top: s - 10 + "px",
                display: "block"
            }, this.setData({
                clickWordTapNum: ++this.data.clickWordTapNum,
                clickWordXYList: a,
                clickWordPointList: o
            });
        },
        _refresh: function() {
            var t = this, e = t.data.opt.baseUrl + "/captcha/get", a = "";
            a = "blockPuzzle" == t.data.opt.captchaType ? wx.getStorageSync("slider") : wx.getStorageSync("point");
            var o = {
                captchaType: t.data.opt.captchaType,
                clientUid: a,
                ts: Date.now()
            };
            console.log(o), t._postData(e, o, function(e) {
                if ("0000" == e.data.repCode) {
                    var a = "blockPuzzle" == o.captchaType ? "向右滑动完成验证" : "请依次点击【" + e.data.repData.wordList.join(",") + "】";
                    t.setData({
                        originalImageBase64: "data:image/png;base64," + e.data.repData.originalImageBase64,
                        jigsawImageBase64: "data:image/png;base64," + e.data.repData.jigsawImageBase64,
                        secretKey: e.data.repData.secretKey,
                        token: e.data.repData.token,
                        leftBarClass: "status-1",
                        verifyTipsClass: "",
                        verifyTipsText: "",
                        verifyMsgText: a,
                        verifyBarAreaClass: "",
                        clickWordTapName: "blockPuzzle" == o.captchaType ? "" : "_clickWordTap",
                        clickWordTapNum: 0,
                        clickWordXYList: [],
                        clickWordPointList: [ {
                            left: 0,
                            top: 0,
                            display: "none"
                        }, {
                            left: 0,
                            top: 0,
                            display: "none"
                        }, {
                            left: 0,
                            top: 0,
                            display: "none"
                        }, {
                            left: 0,
                            top: 0,
                            display: "none"
                        }, {
                            left: 0,
                            top: 0,
                            display: "none"
                        } ]
                    });
                } else t.setData({
                    leftBarClass: "status-3",
                    verifyTipsClass: "err-bg",
                    verifyTipsText: e.data.repMsg
                }), setTimeout(function() {
                    t._refresh();
                }, 700);
            });
        },
        _uuid: function() {
            for (var t = [], e = 0; e < 36; e++) t[e] = "0123456789abcdef".substr(Math.floor(16 * Math.random()), 1);
            t[14] = "4", t[19] = "0123456789abcdef".substr(3 & t[19] | 8, 1), t[8] = t[13] = t[18] = t[23] = "-";
            var a = "slider-" + t.join(""), o = "point-" + t.join("");
            wx.getStorageSync("slider") || wx.setStorageSync("slider", a), wx.getStorageSync("point") || wx.setStorageSync("point", o);
        },
        _postData: function(t, e, a) {
            wx.request({
                url: t,
                data: JSON.stringify(e),
                method: "POST",
                enableCache: !1,
                header: {
                    "Content-Type": "application/json;charset=UTF-8"
                },
                success: function(t) {
                    var e = t.statusCode;
                    200 != e ? wx.showToast({
                        title: "响应错误:\t" + e,
                        icon: "error",
                        duration: 2e3
                    }) : "function" == typeof a && a(t);
                },
                fail: function(t) {
                    console.log(t), wx.showToast({
                        title: "网络错误",
                        icon: "error",
                        duration: 2e3
                    });
                }
            });
        },
        _extend: function(t, e) {
            var a = {};
            for (var o in t) a[o] = t[o];
            for (var o in e) a[o] = e[o];
            return this.setData({
                opt: a
            }), a;
        }
    }
});